# -*- coding: utf-8 -*-

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from appInfo.SQLModel import AppInfoTable
import datetime

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

save_file_name = "E:\\PycharmPerProject\\appInfo\\app_name\\test_app_info_result.txt"


class AppinfoPipeline(object):
    def process_item(self, item, spider):
        with open(save_file_name, 'a+') as fp:
            fp.write(item['src_app_name'] + '|' +
                     ",".join(item['search_app_name']) + '|' +
                     item['search_pkg_name'] + '|' +
                     ",".join(item['app_classification']) + '|' +
                     ",".join(item['user_score']) + '|' +
                     ",".join(item['downloads']) + '|' +
                     str(item['comment_number']) + '|' +
                     ",".join(item['version']) + '|' +
                     item['update_time'] + '|' +
                     ",".join(item['developer']) + '|' +
                     ",".join(
                         [i.replace("\n", "").replace("\r", "").replace("\t", "").strip() for i in item['app_info']
                          if i.replace("\n", "").replace("\r", "").replace("\t", "").strip()]) + '|' + '\n')
            return item


class SQLitePipeline:

    def __init__(self):
        self.session = None

    # open database
    def open_spider(self, spider):
        db_name = spider.settings.get("SQLITE_DB_NAME", 'scrapy.db')
        engine = create_engine("sqlite:///" + db_name, echo=True)
        self.session = sessionmaker(bind=engine)()

    # close database
    def close_spider(self, spider):
        self.session.close()

    #
    def process_item(self, item, spider):
        info = AppInfoTable(src_app_name=item['src_app_name'],
                            search_app_name=",".join(item['search_app_name']),
                            search_pkg_name=item['search_pkg_name'],
                            app_classification=",".join(item['app_classification']),
                            user_score=",".join(item['user_score']),
                            downloads=",".join(item['downloads']),
                            comment_number=str(item['comment_number']),
                            version=",".join(item['version']),
                            update_time=item['update_time'],
                            developer=",".join(item['developer']),
                            app_info=",".join(
                                [i.replace("\n", "").replace("\r", "").replace("\t", "").strip() for i in
                                 item['app_info']
                                 if i.replace("\n", "").replace("\r", "").replace("\t", "").strip()]),
                            add_time=datetime.datetime.now().strftime("%Y%m%d %H:%M"))
        self.session.add(info)
        self.session.commit()
